package com.zjw.hyphenateui.helper;

import com.example.havi.bean.HyChatUser;
import com.example.havi.helper.ChatHelper;
import com.example.havi.util.ChatSPUtil;
import com.example.havi.util.LogUtil;
import com.zjw.hyphenateui.MainApplication;
import com.zjw.hyphenateui.bean.UserBean;
import com.zjw.hyphenateui.ui.MainActivity;
import com.zjw.hyphenateui.util.GsonUtils;

public class UserHelper {

    private static final String CURRENT_USER_INFO = "current_user_info";
    /**
     * 登录执行 发送登录成功消息
     * @param userBean
     */
    public static void login(UserBean userBean) {
        saveCurrentUserInfo(userBean);
        //设置别名
//        JPushHelper.setAlias(UserHelper.getUser().getPush_id());
    }

    /**
     * 退出登录执行 发送退出消息
     */
    public static void loginOut() {
        clearCurrentUserInfo();
        MainActivity.getChatHelper().loginOutTIM(new ChatHelper.LoginOutCallBack() {
            @Override
            public void loginOutSuccess() {

            }

            @Override
            public void loginOutFailure(String errorMsg) {

            }
        });
    }

    /**
     * 清除缓存登录用户信息
     */
    public static void clearCurrentUserInfo() {
        ChatSPUtil.setString(CURRENT_USER_INFO,"");
    }


    /**
     * 缓存登录用户信息 这里只在登录调用
     * @param bean
     */
    public static void saveCurrentUserInfo(UserBean bean) {
        LogUtil.e("frank","保存的登录信息:  "+ GsonUtils.serializedToJson(bean));
        ChatSPUtil.setString(CURRENT_USER_INFO, GsonUtils.serializedToJson(bean));
        syncChatUserInfo();
    }

    /**
     * 同步个人信息
     * @param bean
     */
    public static void syncCurrentUserInfo(UserBean bean) {
        LogUtil.e("frank","同步个人信息:  "+ GsonUtils.serializedToJson(bean));
        UserBean user = getUser();  //同步缓存用户信息
        if(user==null){
            return;
        }
        //这里 一般个人信息接口不返回key 的标识性字段 ，同步时处理下几个关键字段 其他用个人接口覆盖原来信息
        user.setUserId(user.getUserId());
//        user.setPublish_id(user.getPublish_id());
        ChatSPUtil.setString(CURRENT_USER_INFO, GsonUtils.serializedToJson(user));
        syncChatUserInfo();
    }

    /**
     * 获取缓存登录用户信息
     */
    public static UserBean getUser() {
        UserBean userBean = GsonUtils.jsonToBean(ChatSPUtil.getString(CURRENT_USER_INFO, ""), UserBean.class);
        return userBean==null?new UserBean():userBean;
    }
    
    
    /**
     * 同步聊天信息
     */
    
    public static void syncChatUserInfo(){
        HyChatUser hyChatUser = new HyChatUser();
        hyChatUser.setUserId(UserHelper.getUser().getUserId());
        hyChatUser.setNickName(UserHelper.getUser().getNickName());
        hyChatUser.setChatUserName(UserHelper.getUser().getChatId());
        hyChatUser.setChatPassword(UserHelper.getUser().getChatPwd());
        hyChatUser.setAvatar(UserHelper.getUser().getAvatar());
        hyChatUser.setCompany("公司");
        hyChatUser.setJob("职业");
        MainActivity.getChatHelper().syncChatUserInfo(hyChatUser);
    }
    
    
    
}
